#region File Header
//----------------------------------------------------------------
// Copyright (C) 2002 Stefan Kyntchev
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY 
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR 
// FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------
#endregion File Header

using System;
using System.Xml;
using System.Xml.Serialization;

namespace AirWeb.Security
{
	/// <summary>
	/// Represents a User - (security principal)
	/// </summary>
	/// 
	[Serializable]
	public class User
	{
		string m_Name;
		string m_Password;
		string m_Email;
		Role m_Role;
		object m_ExtraData;

		/// <summary>
		/// Creates a new User for the system
		/// </summary>
		/// <param name="i_name">Login name for the user</param>
		/// <param name="i_password">Password</param>
		/// <param name="i_email">e-mail addrees of the user</param>
		/// <param name="i_role">Role for thus user</param>
		/// <param name="i_extraData">Extra application specific data. null if no data is needed</param>
		internal User(string i_name, string i_password, string i_email, Role i_role, object i_extraData)
		{
			m_Name = i_name.ToLower();
			m_Password = EcryptPassword(i_password);
			m_Role = i_role;
			m_Email = i_email.ToLower();
			m_ExtraData = i_extraData;
		}

		/// <summary>
		/// Verifies that the supplied password is a valid password for the user
		/// </summary>
		/// <param name="i_password"></param>
		/// <returns></returns>
		internal bool ValidatePassword(string i_password)
		{
			if(	m_Password == EcryptPassword(i_password))
				return true;
			else
				return false;
		}

		/// <summary>
		/// Encrypts the user password for storage (possible one way)
		/// </summary>
		/// <param name="i_password">password to be encrypted</param>
		/// <returns>encrypted password</returns>
		string EcryptPassword(string i_password)
		{
				return i_password;
		}

		/// <summary>
		/// Decrypts the user password
		/// </summary>
		/// <param name="i_password">password to be decrypted</param>
		/// <returns>decrypted password</returns>
		string DecryptPassword(string i_password)
		{
			return i_password;
		}

		/// <summary>
		/// Name of the user (LoginName)
		/// </summary>
		public string Name
		{
			get
			{
				return m_Name;
			}
		}

		/// <summary>
		/// E-mailAddress of the user
		/// </summary>
		public string Email
		{
			get
			{
				return m_Email;
			}
		}

		/// <summary>
		/// The role of the user
		/// </summary>
		public Role UserRole
		{
			get
			{
				return m_Role;
			}
			set
			{
				m_Role = value;
			}
		}

		/// <summary>
		/// Additional Application Specific data about the user
		/// </summary>
		public object ExtraData
		{
			get
			{
				return m_ExtraData;
			}
		}

		/// <summary>
		/// Access to the users password. This is an unencrypted version of the password
		/// </summary>
		public string Password
		{
			get
			{
				return DecryptPassword(m_Password);
			}
			set
			{
				m_Password = EcryptPassword(value);
			}
		}

		public static User Administrator
		{
			get
			{
				foreach( User u in SecurityMgr.Instance.Users)
				{
					if(u.IsAdministrator())
						return u;
				}

				return null;
			}
		}

		/// <summary>
		/// True if the user has Administrator Role
		/// </summary>
		/// <returns></returns>
		public bool IsAdministrator()
		{
			if(m_Role.Name.ToLower() == "administrator")
				return true;
			else
				return false;			
		}
	}
}
